package com.shanghaionstar.carmanage.service;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.onstar.cn.api.ThirdpartyService;
import com.onstar.cn.chargepile.entity.ChargePileReqEntity;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.shanghaionstar.carmanage.model.ChargePileStationEntity;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NavHolder;
import com.shanghaionstar.utils.NetConnectUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by onstarios on 16/8/1.
 */
public class ChargePileStationService extends BaseService<List<ChargePileStationEntity>> {
    private Context context;
    private int pageNumber = 0;

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public ChargePileStationService(Context context) {
        this.context = context;
    }

    public Observable<ChargePileReqEntity> getCurrentLocaiton(final String query, final String... args) {
        return Observable.defer(new Func0<Observable<ChargePileReqEntity>>() {
            @Override
            public Observable<ChargePileReqEntity> call() {
                try {
                    if(!CommonUtil.isNull(NavHolder.getInstance(context.getApplicationContext()).getMyLocation())) {
                        String latitude = NavHolder.getInstance(context.getApplicationContext()).getMyLocation().getLatitude() + "";
                        String longitude = NavHolder.getInstance(context.getApplicationContext()).getMyLocation().getLongitude() + "";
                        if (!CommonUtil.isNull(latitude) && !CommonUtil.isNull(longitude)) {
                            ChargePileReqEntity entity = new ChargePileReqEntity();
                            entity.setLatitude(latitude);
                            entity.setLongitude(longitude);
                            entity.setQuery(query);
                            if(args != null && args.length>0){
                                entity.setSupplier(args[0]);
                                entity.setCity(NavHolder.getInstance(context.getApplicationContext()).getCurrentCity());
                            }
                            return Observable.just(entity);
                        } else {
                            return null;
                        }
                    }else{
                        return null;
                    }
                } catch (Exception e) {
                    return Observable.error(e);
                }
            }
        });
    }



    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public Observable<List<ChargePileStationEntity>> getChargingPileList(final ChargePileReqEntity entity) {
        return Observable.defer(new Func0<Observable<List<ChargePileStationEntity>>>() {
            @Override
            public Observable<List<ChargePileStationEntity>> call() {
                try {
                    ThirdpartyService service = new ThirdpartyService(Constants.CURRENT_SERVER_URL);
                    RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
                    requestEntity = service.getChargeList(requestEntity, entity, pageNumber);
                    requestEntity = HttpInjectUtils.injectHttp(ChargePileStationService.this.getClass(), "getChargingPileList", requestEntity, ChargePileReqEntity.class);
                    String json = BusinessUtil.getResponse(getDatas(requestEntity), context);
                    return Observable.just((List<ChargePileStationEntity>) new Gson().fromJson( new JSONObject(json).getJSONArray("stations").toString(),new TypeToken<ArrayList<ChargePileStationEntity>>(){}.getType()));
                } catch (Exception e) {
                    return Observable.error(e);
                }
            }
        });
    }


    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public List<ChargePileStationEntity> jsonToBean(String json) {
        return null;
    }
}
